<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-10-13
 * Time: 02:28
 */

namespace app\applet\controller;


use app\applet\BaseController;

class GdzyzController extends BaseController
{
    /**
     * @return \think\response\Json
     */
    public function schoolListAction()
    {
        try {
            $post = $this->get;

            $params['order'] = ['schoolId'=>'asc'];

            if(!empty($post['schoolName'])){
                $params['where'][] = ['schoolName','like','%'.$post['schoolName'].'%'];
            }

            $schools = $this->app->gdzyzSchoolService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($schools as $key => $vo){
                $data['lists'][] = [
                    'schoolId' => $vo->schoolId,
                    'schoolName' => $vo->schoolName,
                    'missionId' => $vo->missionId,
                    'subject' => $vo->mission?$vo->mission->subject:'',
                    'createdAt' => $vo->createdAt,
                ];
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($schools);

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function detailAction($id)
    {
        try {
            $config = $this->app->gdzyzService->getConfig();

            if ($config['activityId'] != $id) {
                throw new \Exception('活动不存在', 500);
            }

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $config]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function missionAction($id)
    {
        try {
            $school = $this->app->gdzyzSchoolService->getDetail($id);

            $config = $this->app->gdzyzService->getConfig();

            $data = [
                'schoolId' => $school->schoolId,
                'schoolName' => $school->schoolName,
                'missionId' => $school->mission->missionId,
                'subject' => $school->mission->subject,
                'signinCode' => $school->mission->signinCode,
                'signoutCode' => $school->mission->signoutCode,
                'activityDesc' => $config['activityDesc'],
            ];

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }
}